package io.github.kouleen.minecraft.plugin.redpacket.command;

import io.github.kouleen.minecraft.core.lang.annotation.MinecraftPluginAutoInject;
import io.github.kouleen.minecraft.core.lang.annotation.MinecraftPluginCommand;
import io.github.kouleen.minecraft.plugin.redpacket.entity.RedPacket;
import io.github.kouleen.minecraft.plugin.redpacket.manager.RedPacketManager;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;

import java.util.Collections;
import java.util.List;

@MinecraftPluginCommand({"redpacket","redp"})
public class RedPacketCommand implements TabExecutor {

    @MinecraftPluginAutoInject
    private RedPacketManager redPacketManager;

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        // 只允许玩家执行命令
        if (!(sender instanceof Player)) {
            sender.sendMessage("§c只有玩家可以使用红包命令!");
            return true;
        }
        Player player = (Player) sender;
        // 处理命令
        if (args.length == 0) {
            sendHelpMessage(player);
            return true;
        }
        switch (args[0].toLowerCase()) {
            case "send":
                handleSendCommand(player, args);
                break;
            case "claim":
                handleClaimCommand(player, args);
                break;
            case "help":
                sendHelpMessage(player);
                break;
            default:
                player.sendMessage("§c未知命令，请使用 /redpacket help 查看帮助");
        }
        return true;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        return Collections.emptyList();
    }

    /**
     * 处理发送红包命令
     * 格式: /redpacket send <金额> <数量> [消息] [过期时间(秒)] [类型(r/f)]
     */
    private void handleSendCommand(Player player, String[] args) {
        if (args.length < 3) {
            player.sendMessage("§c用法: /redpacket send <金额> <数量> [消息] [过期时间(秒)] [类型(r随机/f固定)]");
            return;
        }

        try {
            // 解析金额和数量
            int amount = Integer.parseInt(args[1]);
            int count = Integer.parseInt(args[2]);

            // 解析可选参数
            String message = args.length >= 4 ? args[3] : "";
            long expireSeconds = args.length >= 5 ? Long.parseLong(args[4]) : 300; // 默认5分钟
            RedPacket.RedPacketType type = (args.length >= 6 && args[5].equalsIgnoreCase("f"))
                    ? RedPacket.RedPacketType.FIXED
                    : RedPacket.RedPacketType.RANDOM;

            // 限制过期时间
            if (expireSeconds < 60) expireSeconds = 60;
            if (expireSeconds > 3600) expireSeconds = 3600;
            // 创建红包
            redPacketManager.createRedPacket(player, amount, count, message, expireSeconds, type);
        } catch (NumberFormatException e) {
            player.sendMessage("§c金额和数量必须是数字!");
        }
    }

    /**
     * 处理领取红包命令
     * 格式: /redpacket claim <红包ID>
     */
    private void handleClaimCommand(Player player, String[] args) {
        if (args.length < 2) {
            player.sendMessage("§c用法: /redpacket claim <红包ID>");
            return;
        }

        redPacketManager.claimRedPacket(player, args[1]);
    }

    /**
     * 发送帮助消息
     */
    private void sendHelpMessage(Player player) {
        player.sendMessage("§6===== 红包插件帮助 =====");
        player.sendMessage("§e/redpacket send <金额> <数量> [消息] [过期时间] [类型]");
        player.sendMessage("§7- 发送红包，类型: r(随机) f(固定)，默认随机");
        player.sendMessage("§e/redpacket claim <红包ID>");
        player.sendMessage("§7- 领取指定ID的红包");
        player.sendMessage("§e/redpacket help");
        player.sendMessage("§7- 显示帮助信息");
        player.sendMessage("§6======================");
    }
}
